// pages/home/price/price_list.js
var util = require('../../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    waterdj:0,
    eledj:0,
    meiqidj:0,
    reshuifeidj:0,
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getStorage({
      key: 'userInfo',
      success: function (res) {
        that.setData({
          userInfo: res.data
        })
        util.getReq("/app/sdm/findPrice", {
          source: res.data.source,
          uid: res.data.user_id,
        }, function (datas) {

          if (datas.code == 0) {
            var dats = datas.data;
            if (dats){
              for (var i = 0; i < dats.length; i++) {
                if (dats[i].price_type == 5) {
                  that.setData({
                    waterdj: dats[i].default_price
                  })
                }
                if (dats[i].price_type == 6) {
                  that.setData({
                    eledj: dats[i].default_price
                  })
                }

                if (dats[i].price_type == 7) {
                  that.setData({
                    meiqidj: dats[i].default_price
                  })
                }
                if (dats[i].price_type == 8) {
                    that.setData({
                      reshuifeidj: dats[i].default_price
                    })
                }
              }
            }
            
            that.setData({
              dataList: datas.data
            })

          } else {
            wx.showToast({
              title: datas.desc,
            })
          }

        })
      }, fail: function () {
        wx.redirectTo({
          url: '/pages/index/index',
        })
      }
    });
  },

  
  /**
   * 水费
   */
  waterinput:function(e){
      console.log(e.detail.value)
      var that=this;
      that.setData({
        waterdj: e.detail.value,
      })
  },
  /**
   * 电费
   */
  eleinput:function(e){
    var that = this;
    that.setData({
      eledj: e.detail.value,
    })
  },
  /**
   * 煤气费
   */
  meiqiinput: function (e) {
    var that = this;
    that.setData({
      meiqidj: e.detail.value,
    })
  },
  /**
   * 热水费
   */
  reshuiinput: function (e) {
    var that = this;
    that.setData({
      reshuifeidj: e.detail.value,
    })
  },
  /**
   * 保存
   */
  submit:function(e){
    var that=this;
      wx.showModal({
        title: '提示',
        content: '确定保存吗?',
        success:function(res){
            if(res.confirm){
                wx.showLoading({
                  title: '保存中请稍后...',
                  mask:true,
                })
                that.savesubmit(that);
                
            }
        }
      })
  },
  /**
   * 保存请求
   */
  savesubmit:function(that){
    let reg = new RegExp("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$");
    if (!reg.test(that.data.waterdj) || !reg.test(that.data.eledj) || !reg.test(that.data.meiqidj) || !reg.test(that.data.reshuifeidj)){
      wx.showToast({
        title: '请输入最多2位小数的金额',
        icon: 'none'
      })
      return false;
    }
    util.getReq("/app/sdm/addOrUpdateS", {
      source: that.data.userInfo.source,
      sf: that.data.waterdj,
      df: that.data.eledj,
      mqf: that.data.meiqidj,
      rsf: that.data.reshuifeidj,
      user_type_id: that.data.userInfo.user_type_id,
      uid: that.data.userInfo.user_id,
      phone_num: that.data.userInfo.phone_num,
      user_name: that.data.userInfo.user_name,
    }, function (datas) {
      wx.hideLoading();
      
        if(datas.code==0){
            wx.showToast({
              title: '修改成功',
              icon: 'none'
            })
        }else{
          var errMsg='服务器异常';
          if (datas.desc){
            errMsg = datas.desc;
          }
          wx.showToast({
            title: errMsg,
            icon:'none'
          })
        }
    })
  
  },
  /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh(true);
  },

})